<template>
	<div>
		<ApplyReport :nowRowData="nowRowData" :creditCode="nowRowData.creditCode" ref="applyReport">
			<el-scrollbar max-height="50vh" v-if="lineTimeList.length>0">
				<div class="progress-box">
					<div class="time-line" v-loading="processLoad">
						<div class="time-line-item" v-for="(item, index) in lineTimeList" :key="index">
							<div class="time-line-item-header flex space-between align-center">
								<p class="text-time">{{ formatCreateTime(item.createTime) }}</p>
								<div :class="item.approvalStatus == 3 ? 'text-error-tag' : 'text-success-tag'" v-if="item.statusLabel">{{ item.statusLabel }}</div>
							</div>
							<p class="text-title">{{ approvalHierarchyLabel[item.approvalHierarchy] ||item.createUserName }} </p>
							<p class="text-value">{{ item.approvalRemarks }}</p>
              <p class="text-value" v-if="item.currentApproval">节点审批人：{{ item.currentApprovalUserName }}</p>
              <FileList v-if="item.approvalImgs" :value="item.approvalImgs" class="mt10"/>
						</div>
					</div>
				</div>
			</el-scrollbar>
		</ApplyReport>
    <div class="footer-btn fc mt10" v-if="nowRowData.approvalStatus == 1 || nowRowData.approvalStatus == -1">
      <el-button  type="primary" @click="handleSubmitApply">提交</el-button>
    </div>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import ApplyReport from '../RuleForm/ApplyReport/index.vue'
import { onMounted } from 'vue'
import FileList from '@/views/aplid/DetailPage/ContentListItem/FileList/index.vue'
import { useDateFormat } from '@vueuse/core'

const props = defineProps<{
	nowRowData: any
}>()


const emit = defineEmits(['refresh'])

let applyReport = ref()
const statusLabel = {
	'-1': '作废',
	'0': '待审批',
	'1': '审批中',
	'2': '通过',
	'3': '驳回',
}
const approvalHierarchyLabel = {
	'1': '社区',
	'2': '街道',
	'3': '区',
	'4': '市',
	'5': '第三方',
}
let processLoad = ref(true)
const lineTimeList = ref([])

const formatCreateTime = (time) => {
	const ntme = useDateFormat(time, 'YYYY年MM月DD日 HH:mm:ss')
	return ntme.value
}

const getInitData = async() => {
	let params = {
		fromId: props.nowRowData.id,
		randomNo: props.nowRowData.workflowRandom,
	}
	apiMod.view_workflowApprovalLogic_process(params).then((res) => {
		lineTimeList.value = (res.data || []).map((item) => {
			return {
				...item,
				statusLabel: statusLabel[item.approvalStatus],
			}
		}).reverse().filter(item=>item.id)
		processLoad.value = false
	})
}
const handleSubmitApply = async()=> {
// todo保存表单结果数据，提交时保存一次，不在调
let data = props.nowRowData.ofsSubsidyApplyElderlyPO
let date = props.nowRowData.belongDate.split("-")

let model = {
	oldmanName:data.oldmanName,
	organizationLogo:data.organizationLogo,
	genderLabel:data.genderLabel,
	nationalityLabel:data.nationalityLabel,
	idCard:data.idCard,
	oldmanPhone:data.oldmanPhone,
	crProvinceName:data.crProvinceName,
	crCityName:data.crCityName,
	crAreaName:data.crAreaName,
	crStreetName:data.crStreetName,
	crCommunityName:data.crCommunityName,
	crAddress:data.crAddress,
	provinceName:data.provinceName,
	cityName:data.cityName,
	areaName:data.areaName,
	streetName:data.streetName,
	communityName:data.communityName,
	address:data.address,
	oldmanImage:data.oldmanImage
}
let tableRel = []
if(props.nowRowData.tableRel&&typeof props.nowRowData.tableRel=='string'){
	tableRel = JSON.parse(props.nowRowData.tableRel)
}
let params0 = {
	applyYear:date[0],
	applyMonth:date[1],
	dataContent: model,
	dataResults: model,
	fromId:props.nowRowData.applyNo,
	fromType: tableRel[0].value,
	tableTemplateId: tableRel[0].value,
	projectId: props.nowRowData.subsidyProjectId,
}
await apiMod.view_formResults_add(params0)
// 保存申请老人的数据，审批通过后不在调用
await apiMod.view_ofsSubsidyApplyElderly_edit(props.nowRowData.ofsSubsidyApplyElderlyPO)


  const params = {
    fromId: props.nowRowData.id,
    randomNo: props.nowRowData.workflowRandom,
    areaId: props.nowRowData.areaId,
  }
  let res = await apiMod.view_workflowApprovalLogic_submit(params)
  ElMessage.success(res.data)
  emit('refresh')
  
}

watch(()=>props.nowRowData,(newVal)=>{
	if(newVal&&newVal.id){
		getInitData()
	}
},{
	immediate:true,
	deep:true
})
</script>

<style scoped lang="scss">
@import '@/views/instOperate/components/styles/progress.scss';
</style>
